import CustomLoading from 'components/custom-loading';
import CategoryList from './category_list';
import { Toast } from 'vant';

export default {
  components: { CustomLoading, CategoryList },
  data() {
    return {
      items: [],
      subItems: [],
      mainActiveIndex: 0,
      parentId: 0,
      currentIndex: 0,
      // 请求参数对象
      queryObj:{
        page: 1,
        page_size: 10
      },
      // 总数量，用来实现分页
      total: 0,
      // 是否正在请求数据
      isloading: false,
      finished: false,
      refreshing: false
    };
  },
  methods: {
    /**
     * 搜索框获取焦点跳转到搜索页面
     */
    toSearch: function() {
      this.$router.push(this.fun.getUrl("search"));
    },

    /**
     * 获取分类
     */
    getClassify: function() {
      const that = this;
      this.items = [{id: 0, text: '推荐品牌'}];
      this.subItems = [];
      $http.get("goods.classify.home").then(
        response => {
          const { result, data, msg } = response;
          if (result == 1) {
            if (data.categoryList && data.categoryList.data && data.categoryList.data.length) {
              const list = data.categoryList.data.map(item => {
                return {
                  id: item.id,
                  text: item.name
                };
              });
              that.items = [...that.items, ...list];
              that.getSubClassify();
            }
          } else {
            Toast(msg || '获取分类数据失败！');
          }
        }
      );
    },

    /**
     * 获取子类
     */
    getSubClassify: function() {
      const { queryObj, isloading, parentId } = this;
      if (isloading) {
        return;
      }
      this.isloading = true;
      const { page, page_size } = queryObj;
      const json = { parent_id: parentId, page, page_size };
      const that = this;
      const url = parentId ? "goods.category.get-children-category" : "goods.brand.get-brand";
      $http.get(url, json).then(
        response => {
          that.isloading = false;
          const { result, data, msg } = response;
          if (result == 1) {
            that.subItems.push(...data.data);
            that.finished = that.subItems.length >= data.total;
          } else {
            Toast(msg || '获取子类数据失败！');
          }
        }
      ).finally(() => {
        that.isloading = false;
        that.refreshing = false;
        that.mainActiveIndex = that.currentIndex;
      });
    },

    /**
     * 重置页面请求数据
     */
    resetQuery: function() {
      this.queryObj = {
        page: 1,
        page_size: 10
      };
      this.total = 0;
      this.isloading = false;
      this.finished = false;
      this.subItems.splice(0, this.subItems.length);
    },

    /**
     * 子类下拉刷新
     */
    refreshSubItems: function() {
      if(this.isloading) {
        return;
      }
      this.resetQuery();
      this.refreshing = true;
      this.getSubClassify();
    },

    /**
     * 分类点击获取子类
     * @param {Number} index 索引
     */
    onClickNav: function(index) {
      const { id } = this.items[index];
      this.currentIndex = index;
      this.parentId = id;
      this.subItems.splice(0, this.subItems.length);
      this.getSubClassify();
    },

    /**
     * 子类点击跳转
     * @param {Number} id 分类id
     */
    gotoCateList: function(id) {
      this.$router.push(this.fun.getUrl('catelist', { id }));
    }
  },
  activated() {
    this.getClassify();
  }
};